package egovframework.npqs.pqi.mobile.jsonparser;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import egovframework.npqs.pqi.mobile.AppStatus;
import egovframework.npqs.pqi.mobile.common.Const;
import egovframework.npqs.pqi.mobile.jsoncontent.ErrorItem;
import egovframework.npqs.pqi.mobile.jsoncontent.ExportFruitSearchItem;
import egovframework.npqs.pqi.mobile.jsoncontent.PlaceOfficerItem;
import egovframework.npqs.pqi.mobile.jsonparser.PlaceOfficerThread.PlaceOfficerThreadCallback;
import egovframework.npqs.pqi.mobile.util.BrUtilManager;
import egovframework.npqs.pqi.mobile.util.ParameterUtil;

public class ExportFruitSearchThread extends Thread{
	public interface ExportFruitSearchThreadCallback {
		public void OnExportFruitSearchCompleted(boolean isNetworkConntected, List<ExportFruitSearchItem> arrFruitItem);
		public void OnExportFruitSearchError(ErrorItem errorItem);
	}

	private String url;
	private ExportFruitSearchThreadCallback callback;
	private ArrayList<NameValuePair> post;
	private Context context;
	private AppStatus appStatus;
	
	/* 파라메터*/
	private String searchAppoinYear, searchKorNm;
	
	public ExportFruitSearchThread(Context context,
			ExportFruitSearchThreadCallback callback, String searchAppoinYear, String searchKorNm) {
		this.context = context;
		this.callback = callback;
		this.appStatus = (AppStatus) context.getApplicationContext();
		this.searchAppoinYear = searchAppoinYear;
		this.searchKorNm = searchKorNm;
	}

	@Override
	public void run() {
		// 통신불통상태이면
		if(!appStatus.utilm.isNetworkConnected(context)) {
			callback.OnExportFruitSearchCompleted(false, null);
			return;
		}
		
		JSONParser jParser = new JSONParser();

		url = Const.MAIN_URL;
		post = ParameterUtil.getSearchFruitPackingParam(searchAppoinYear, searchKorNm);
		
		JSONObject json = jParser.getJSONFromUrl(url, post);
		Log.i("url","url = " + url + post);
		Log.i("url","json = " + json);
		if(json == null) {	// json 값이 null 일때
			callback.OnExportFruitSearchError(null);
			return;
		}

		try {		
			int result = json.getInt(Const.RESULT);
			Log.i("result","result = " + result);
			ArrayList<ExportFruitSearchItem> arrFruitItem = new ArrayList<ExportFruitSearchItem>();
			
			if(result == 0){	// result 값이 0일 경우
				String data = json.getString(Const.DATA);
				
				ErrorItem errorItem = new ErrorItem();
				errorItem.setrDesc(data);
				callback.OnExportFruitSearchError(errorItem);
				
			}else{	// result 값이 0이 아닐 경우
				JSONArray arrayLogin = json.getJSONArray(Const.DATA);
				Log.i("arrayLogin","arrayLogin = " + arrayLogin);
				ExportFruitSearchItem fruitItem = null;
				for (int i = 0; i < arrayLogin.length(); i++) {
					JSONObject prod = arrayLogin.getJSONObject(i);
					
					fruitItem = new ExportFruitSearchItem();
					fruitItem.setExportAreaCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, ExportFruitSearchItem.EXPORTAREACD)));
					fruitItem.setAppointYear(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, ExportFruitSearchItem.APPOINTYEAR)));
					fruitItem.setAreaNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, ExportFruitSearchItem.AREANM)));
					fruitItem.setCountryNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, ExportFruitSearchItem.COUNTRYNM)));
					fruitItem.setFruitNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, ExportFruitSearchItem.FRUITNM)));
					fruitItem.setKorNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, ExportFruitSearchItem.KORNM)));
					fruitItem.setOffNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, ExportFruitSearchItem.OFFNM)));
					fruitItem.setZipcode(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, ExportFruitSearchItem.ZIPCODE)));
					fruitItem.setLnmAdres(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, ExportFruitSearchItem.LNMADRES)));
					fruitItem.setLnmAdresDetail(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, ExportFruitSearchItem.LNMADRESDETAIL)));
					
					arrFruitItem.add(fruitItem);
					callback.OnExportFruitSearchCompleted(true, arrFruitItem);
				}
				
			}
		} catch (JSONException e) {
		}
	}
}
